package com.itheimamobileguard.activitys;

import com.example.mobile.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LostFindActivity extends Activity {
	
	private static final String TAG = "LostFindActivity";
	private SharedPreferences sp;
	private TextView tv_lostfind_number;
	private ImageView im_lostfind_status;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		if (isFinishSetup()) {
			setContentView(R.layout.activity_lost_find);
			tv_lostfind_number = (TextView) findViewById(R.id.tv_lostfind_number);
			im_lostfind_status = (ImageView) findViewById(R.id.im_lostfind_status);
			tv_lostfind_number.setText(sp.getString("safeNum", ""));
			
			boolean protecting = sp.getBoolean("protecting", false);
			if (protecting) {
				im_lostfind_status.setImageResource(R.drawable.key);
			}else{
				im_lostfind_status.setImageResource(R.drawable.ic_menu_close_clear_cancel);
			}
			Log.i(TAG, "设置过，进入");
		}else{
			Log.i(TAG, "没有设置过");
			Intent intent = new Intent(LostFindActivity.this,Setup1Activity.class);
			startActivity(intent);
			finish();
		}
		
	}
	
	
	
	/**
	 * 判断是否设置过向dao
	 */
	private boolean isFinishSetup(){
		return sp.getBoolean("finishSet", false);
	}
	
	public void resSetUp(View view){
		Intent intent = new Intent(LostFindActivity.this,Setup1Activity.class);
		startActivity(intent);
		finish();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//getMenuInflater().inflate(R.menu.lostfind, menu);
		return true;
	}
	
	//当菜单被用户打开的时候
	
	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		Toast.makeText(this, "呵呵", 0).show();
		ProgressDialog pb = new ProgressDialog(LostFindActivity.this);
		pb.setTitle("abc");
		pb.show();
		return super.onMenuOpened(featureId, menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(R.id.it_lostfind_name == item.getItemId()){
			AlertDialog.Builder builder = new Builder(LostFindActivity.this);
			final EditText ed = new EditText(LostFindActivity.this);
			builder.setTitle("请输入名称");
			builder.setView(ed);
			builder.setPositiveButton("确定", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					String name =  ed.getText().toString().trim();
					Editor editor = sp.edit();
					editor.putString("newName1", name);
					editor.commit();
					Toast.makeText(getApplicationContext(), "新的名称："+name, 0).show();
				}
			});
			builder.show();
		}
		return super.onOptionsItemSelected(item);
	}
	
}
